/* GUI CONVERTED from gpmpupb.p (converter v1.75) Thu Aug 24 13:29:24 2000 */
/* gpmpupb.p - RECALCULATE MATERIALS PLAN - Update in_mrp flags to yes  */
/* Copyright 1986-2000 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*V8:ConvertMode=Maintenance                                            */
/*J23R*/ /*V8:RunMode=Character,Windows                                 */
/* REVISION: 7.2       LAST EDIT: 01/17/95      MODIFIED BY: emb *F0GM*      */
/* REVISION: 7.3       LAST EDIT: 05/30/96      MODIFIED BY: emb *G1WY*      */
/* REVISION: 7.4       LAST EDIT: 01/09/97  MODIFIED BY: *H0R2* Russ Witt    */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 04/01/98   BY: *J23R* Sandesh Mahagaokar */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan         */
/* REVISION: 8.6E     LAST MODIFIED: 06/20/00   BY: *L0ZS* Rajesh Thomas      */
/* REVISION: 9.1      LAST MODIFIED: 07/19/00   BY: *N0GB* Mudit Mehta        */
/* REVISION: 9.1      LAST MODIFIED: 08/23/00   BY: *N0ND* Mark Brown         */

/*L0ZS*/ /* CODE MODIFIED TO PREVENT INFINITE LOOPING AFTER PROCESSING   */
/*L0ZS*/ /* 4000 IN_MRP RECORDS AT SAME LOW LEVEL CODE FOR WHICH IN_MRP  */
/*L0ZS*/ /* FLAG HAS NOT BEEN CHANGED FROM NO TO YES                     */

/* ********** Begin Translatable Strings Definitions ********* */

/*N0GB*
 * &SCOPED-DEFINE gpmpupb_p_1 "Updating planning required flags for regeneration"
 * /* MaxLen: Comment: */
 *N0GB*/

/* ********** End Translatable Strings Definitions ********* */

         define input parameter sync_code as character no-undo.

         define shared variable part1 like pt_part no-undo.
         define shared variable part2 like pt_part no-undo.
         define shared variable site1 like in_site no-undo.
         define shared variable site2 like in_site no-undo.
         define shared variable ms like pt_ms no-undo.
         define shared variable non_ms like pt_ms no-undo.
         define shared variable mrp like pt_ms no-undo.
         define shared variable mrp_req like pt_mrp no-undo.
         define shared variable prod_line like pt_prod_line no-undo.
         define shared variable buyer like pt_buyer no-undo.
         define shared variable part_type like pt_part_type no-undo.
         define shared variable ptgroup like pt_group no-undo.
         define shared variable vendor like pt_vend no-undo.
         define shared variable pm_code like pt_pm_code no-undo.
         define shared variable drp like mrpc_drp no-undo.

         define variable in_recid as recid no-undo.
         define variable low_level as integer no-undo.
         define variable max_level as integer no-undo.
         define variable min_level as integer no-undo.

         define variable qad-recno as recid no-undo.
         define variable qadmrpkey as character initial "mrp/drp" no-undo.
         define variable dblcol as character initial "::" no-undo.
         define buffer qadwkfl for qad_wkfl.
         define shared variable mfguser as character.

         define variable i as integer no-undo.
         define variable j as integer no-undo.
         define variable redo-records as recid extent 1000 initial 0 no-undo.
         define variable redo-level as logical no-undo.
         define variable msg01 as character no-undo.
         define variable msg03 as character no-undo.

/*G1WY*/ define variable last_part as character no-undo.
/*G1WY*/ define variable last_site as character no-undo.
/*J23R*/ define variable n_items_proc as integer no-undo.
/*L0ZS*/ define variable prev_part  like in_part  no-undo.
/*L0ZS*/ define variable prev_level like in_level no-undo.

         {mfglobal.i}
/*N0GB*/ {gplabel.i}

/*J23R** find msg_mstr no-lock where msg_lang = global_user_lang
 *       and msg_nbr = 974 no-error.
 *J23R**/

/*J23R*/ for first msg_mstr
/*J23R*/     fields (msg_desc msg_lang msg_nbr)
/*J23R*/     no-lock where msg_lang = global_user_lang
/*J23R*/               and msg_nbr  = 974:
/*J23R*/ end. /* FOR FIRST msg_mstr */

         if available msg_mstr then msg01 = msg_desc + ".".
/*N0GB*  else msg01 = {&gpmpupb_p_1}. */
/*N0GB*/ else msg01 = getTermLabel("UPDATING_PLAN_FLAGS_FOR_REGENERATE",45).

/*J23R** find msg_mstr no-lock where msg_lang = global_user_lang
 *       and msg_nbr = 1106 no-error.
 *J23R**/

/*J23R*/ for first msg_mstr
/*J23R*/     fields (msg_desc msg_lang msg_nbr)
/*J23R*/     no-lock where msg_lang = global_user_lang
/*J23R*/               and msg_nbr  = 1106:
/*J23R*/ end. /* FOR FIRST msg_mstr */

         if available msg_mstr then msg03 = msg_desc.

         repeat:

/*G1WY*     find qad_wkfl exclusive-lock where */
/*G1WY*/    find first qad_wkfl exclusive-lock where
            qad_key3 = qadmrpkey and qad_key4 = mfguser no-error.

            if not available qad_wkfl then
            find qad_wkfl exclusive-lock where qad_key1 = qadmrpkey
            and qad_key2 = mfguser + dblcol + qadmrpkey no-error.

            if not available qad_wkfl then create qad_wkfl.

            assign qad_key1 = qadmrpkey
                   qad_key2 = mfguser + dblcol + "mrpflag"
                   qad_key3 = qadmrpkey
                   qad_key4 = mfguser
                   qad_key5 = qadmrpkey + sync_code
                   qad_key6 = "-99999"
/*G1WY*/           qad_charfld[1] = global_userid
/*G1WY*/           qad_datefld[1] = today
/*G1WY*/            qad_decfld[1] = time
                        qad-recno = recid(qad_wkfl).

            pause 0 no-message.

            leave.
         end.

         max_level = 0.
/*J23R**         find last in_mstr
 *      /*H0R2*/ where in_level >= -99999999 and in_mrp >= no
 *      /*H0R2*/ and in_part >= "" and in_site >= ""
 *               use-index in_level no-lock no-error.
 *J23R**/

/*J23R*/ for last in_mstr
/*J23R*/     fields (in_level in_mrp in_part in_site)
/*J23R*/     where in_level >= -99999999 and in_mrp >= no
/*J23R*/       and in_part >= "" and in_site >= ""
/*J23R*/     use-index in_level no-lock
/*J23R*/ query-tuning(no-index-hint hint "INDEX_DESC(T0 IN_MSTR##IN_LEVEL)"):
/*J23R*/ end. /* FOR LAST in_mstr */

         if available in_mstr then max_level = in_level.

         min_level = 0.
/*J23R**          find first in_mstr
 *       /*H0R2*/ where in_level >= -99999999 and in_mrp >= no
 *       /*H0R2*/ and in_part >= "" and in_site >= ""
 *                use-index in_level no-lock no-error.
 *J23R**/

/*J23R*/ for first in_mstr
/*J23R*/     fields (in_level in_mrp in_part in_site)
/*J23R*/     where in_level >= -99999999 and in_mrp >= no
/*J23R*/     and in_part >= "" and in_site >= ""
/*J23R*/     use-index in_level no-lock:
/*J23R*/ end. /* FOR FIRST in_mstr */

         if available in_mstr then min_level = in_level.

/*L0ZS*/ assign
/*L0ZS*/    prev_part  = part1
/*L0ZS*/    prev_level = min_level.

         /* Read through all relevant in_mstr records and set in_mrp = yes */

         mainloop:
         do low_level = min_level to max_level
/*J23R*/ on error undo, retry:

/*L0ZS*/    if prev_level <> low_level then
/*L0ZS*/       assign
/*L0ZS*/          prev_part  = part1
/*L0ZS*/          prev_level = low_level.

/*J23R*/    if retry then .
            redo-level = no.

            if not can-find (first in_mstr where in_level = low_level)
            then do:
/*J23R**      find first in_mstr no-lock where in_level > low_level no-error. */
/*J23R*/      for first in_mstr
/*J23R*/          fields (in_level in_mrp in_part in_site)
/*J23R*/          no-lock where in_level > low_level:
/*J23R*/      end. /* FOR FIRST in_mstr */
              if available in_mstr then low_level = in_level.
              else leave.
            end.

            hide message no-pause.
            message msg01.
            message msg03 string(low_level) "/" string(max_level).

/*J23R**            find first in_mstr no-lock
 *      /*G1WY*/    use-index in_level
 *                  where in_level = low_level and in_mrp = no
 *                  and (in_part >= part1 and in_part <= part2)
 *                  and (in_site >= site1 and in_site <= site2)
 *                  no-error.
 *J23R**/

 /*J23R*/    define query q_in_mstr for in_mstr
 /*J23R*/    fields(in_level in_mrp in_part in_site).
 /*J23R*/    open query q_in_mstr for each in_mstr no-lock
 /*J23R*/    use-index in_level
 /*J23R*/    where in_level = low_level and in_mrp = no
 /*L0ZS**    /*J23R*/   and (in_part >= part1 and in_part <= part2)  */
 /*L0ZS*/    and (in_part >= prev_part and in_part <= part2)
 /*J23R*/    and (in_site >= site1     and in_site <= site2).

 /*J23R*/    n_items_proc = 0.

 /*J23R*/    release pt_mstr.

            repeat:

/*J23R*/       n_items_proc = n_items_proc + 1.
/*J23R*/       if n_items_proc > 4000 then
/*J23R*/          undo mainloop, retry mainloop.
/*J23R*/       get next q_in_mstr.

               if not available in_mstr then leave.
               in_recid = recid(in_mstr).

/*L0ZS*/       assign
/*L0ZS*/          prev_part  = in_part
/*L0ZS*/          prev_level = low_level.

/*J23R*/       if last_part <> in_part then
/*J23R*/       for first pt_mstr
/*J23R*/           fields (pt_bom_code pt_desc1 pt_ord_pol
/*J23R*/                   pt_buyer pt_group pt_ms pt_part pt_part_type
/*J23R*/                   pt_pm_code pt_prod_line pt_vend)
/*J23R*/           no-lock where pt_part = in_part:
/*J23R*/       end.

/*J23R*/       for first ptp_det
/*J23R*/           fields (ptp_bom_code ptp_buyer ptp_ms ptp_ord_pol
/*J23R*/                   ptp_part ptp_pm_code ptp_site ptp_vend)
/*J23R*/           no-lock where ptp_part = in_part
/*J23R*/                     and ptp_site = in_site:
/*J23R*/       end.

/*G1WY*/       assign last_part = in_part
/*G1WY*/              last_Site = in_site.

/*J23R**       find pt_mstr no-lock where pt_part = in_part no-error.  */

/*J23R**       find ptp_det no-lock where ptp_part = in_part
 *             and ptp_site = in_site no-error.
 *J23R**/

               if available pt_mstr
               and (prod_line = "" or pt_prod_line = prod_line)
               and (ptgroup = "" or pt_group = ptgroup)
               and (part_type = "" or pt_part_type = part_type)

               and ( (available ptp_det
               and ((drp = yes and ptp_pm_code = "D")
                or (mrp = yes and ptp_pm_code <> "D"))
               and ((ms = yes and ptp_ms = yes)
                or (non_ms = yes and ptp_ms = no))
               and (buyer = "" or ptp_buyer = buyer)
               and (vendor = "" or ptp_vend = vendor)
               and (pm_code = "" or ptp_pm_code = pm_code))

               or (not available ptp_det
               and ((drp = yes and pt_pm_code = "D")
                or (mrp = yes and pt_pm_code <> "D"))
               and ((ms = yes and pt_ms = yes)
                or (non_ms = yes and pt_ms = no))
               and (buyer = "" or pt_buyer = buyer)
               and (vendor = "" or pt_vend = vendor)
               and (pm_code = "" or pt_pm_code = pm_code)) )
               then do:

                  find in_mstr exclusive-lock where recid(in_mstr) = in_recid
                  no-wait no-error.
                  if available in_mstr then in_mrp = yes.
                  else if locked(in_mstr) then do:

                     redo-level = yes.

                     if i < 1000 then redo-records[i + 1] = in_recid.
                     i = i + 1.
                  end.
               end.

/*G1WY*        find next in_mstr no-lock
      *        where in_level = low_level and in_mrp = no
      *        and (in_part >= part1 and in_part <= part2)
      *        and (in_site >= site1 and in_site <= site2)
      *        no-error.
**G1WY*/
/*J23R** /*G1WY*/       /* Added section */
 *             find first in_mstr no-lock use-index in_level
 *             where in_level = low_level and in_mrp = no
 *             and in_part = last_part
 *             and (in_site > last_site and in_site <= site2)
 *             no-error.
 *
 *             if not available in_mstr then
 *             find first in_mstr no-lock use-index in_level
 *             where in_level = low_level and in_mrp = no
 *             and (in_part > last_part and in_part <= part2)
 *             and (in_site >= site1 and in_site <= site2)
 *             no-error.
 * /*G1WY*/       /* End of added section */
 *J23R**/

            end.

            if redo-level then do:

               if i <= 1000 then redo-level = no.

               do transaction j = 1 to min(i,1000):

                  if redo-records[j] <> 0 then do:
                     find in_mstr exclusive-lock
                        where recid(in_mstr) = redo-records[j]
                        no-wait no-error.
                     if available in_mstr
                        then assign in_mrp = yes
                           redo-records[j] = 0.
                     else do:
/*J23R**                find in_mstr no-lock
 *                      where recid(in_mstr) = redo-records[j] no-error.
 *J23R**/
/*J23R*/                for first in_mstr
/*J23R*/                    fields (in_level in_mrp in_part in_site)
/*J23R*/                    no-lock where recid(in_mstr) = redo-records[j]:
/*J23R*/                end. /* FOR FIRST in_mstr */
                        if available in_mstr and in_mrp = yes
                        then redo-records[j] = 0.
                        else redo-level = yes.
                     end.
                  end.
               end.
            end.

            if redo-level = yes
               then assign i = 0
                redo-records = 0
                   low_level = low_level - 1.
         end.

         do transaction:
            find qad_wkfl exclusive-lock where recid(qad_wkfl) = qad-recno.

/*G1WY*/    assign qad_wkfl.qad_charfld[1] = global_userid
/*G1WY*/           qad_wkfl.qad_datefld[1] = today
/*G1WY*/            qad_wkfl.qad_decfld[1] = time
                         qad_wkfl.qad_key2 = mfguser + dblcol + qadmrpkey.
         end.
